﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using LTJ.Entity;
using LTJ.Common;
using LTJ.Config;
using LTJ.FrameWork;
using System.Text.RegularExpressions;

namespace LTJ.WebPage.UserManage.user
{
    /// <summary>
    /// 企业用户管理
    /// </summary>
    public partial class corpusergrid : UserPage
    {
        /// <summary>
        /// 管理类型，默认0为全部，1为超级管理员，2为普通管理员，3为用户。
        /// </summary>
        protected int manageType = SASRequest.GetInt("mtype", 0);
        protected int groupid = 0;
        protected string parmStr = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataGrid();
            }
        }

        protected void BindDataGrid()
        {
            string strWhere = " cid = " + companyinfo.id;

            if (manageType == 3) strWhere = " scid = " + companyinfo.id;

            if (ViewState["condition"] == null || ViewState["condition"].ToString() == "")
            {
                if (parmStr != "") strWhere += " AND " + parmStr;
            }
            else
            {
                if (parmStr != "") strWhere += " AND " + ViewState["condition"].ToString() + " AND " + parmStr;
                else strWhere += " AND " + ViewState["condition"].ToString();
            }

            DataGrid1.VirtualItemCount = Users.GetUsersCount(strWhere);
            DataGrid1.DataSource = Users.GetUsersByPage(DataGrid1.CurrentPageIndex + 1, DataGrid1.PageSize, strWhere, "");
            DataGrid1.DataBind();
        }

        protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            DataGrid1.CurrentPageIndex = e.NewPageIndex;
            BindDataGrid();
        }

        protected void GoToPagerButton_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        /// <summary>
        /// 搜索点击
        /// </summary>
        protected void check_Click(object sender, EventArgs e)
        {
            ViewState["condition"] = "";
            string lname = lname_txt.Text;
            string rname = rname_txt.Text;
            string phone = phone_txt.Text;
            int cur_status = TypeConverter.StrToInt(cur_status_txt.SelectedValue, -1);

            string[] constrarray = new string[4];

            if (lname != "" && Utils.IsSafeSqlString(lname)) constrarray[0] = "lname like '%" + lname + "%'";
            else constrarray[0] = "";

            if (rname != "" && Utils.IsSafeSqlString(rname)) constrarray[1] = "rname like '%" + rname + "%'";
            else constrarray[1] = "";

            if (phone != "" && Utils.IsSafeSqlString(phone)) constrarray[2] = "phone like '%" + phone + "%'";
            else constrarray[2] = "";

            if (cur_status > -1) constrarray[3] = "status = " + cur_status;
            else constrarray[3] = "";

            for (int i = 0; i < 4; i++)
            {
                if (String.IsNullOrEmpty(constrarray[i])) continue;

                if (ViewState["condition"] != null && ViewState["condition"].ToString() != "")
                    ViewState["condition"] += " AND " + constrarray[i];
                else
                    ViewState["condition"] = constrarray[i];
            }

            DataGrid1.CurrentPageIndex = 0;
            BindDataGrid();
        }

        /// <summary>
        /// 重新绑定
        /// </summary>
        protected void reclick_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        /// <summary>
        /// 密码重置
        /// </summary>
        protected void reuserpass_ServerClick(object sender, EventArgs e)
        {
            int ruserid = SASRequest.GetInt("ruserid", 0);

            UserInfo ruinfo = Users.GetUser(ruserid);

            if (ruinfo != null && ruinfo.id > 0)
            {
                ruinfo.pass = Utils.MD5(ruinfo.syspass);
                if (Users.UpdateUser(ruinfo))
                {
                    base.RegisterStartupScript("PAGETEMPLATE", "重置成功！");
                }
            }

            OpLogs.CreateCompanyOPLog(userinfo, SASRequest.GetIP(), LTJ.Entity.OpAction.CompanyUserInfoUpdate.ActionID, "重置会员密码-会员名：" + ruinfo.lname);

            BindDataGrid();
        }
        /// <summary>
        /// 停止用户
        /// </summary>
        protected void pause_Click(object sender, EventArgs e)
        {
            if (SASRequest.GetString("cid") != "")
            {
                Users.PauseUsers(SASRequest.GetString("cid"));
                BindDataGrid();
            }
        }

        /// <summary>
        /// 开启用户
        /// </summary>
        protected void start_Click(object sender, EventArgs e)
        {
            if (SASRequest.GetString("cid") != "")
            {
                Users.StartUsers(SASRequest.GetString("cid"));
                BindDataGrid();
            }
        }

        protected void alldel_Click(object sender, EventArgs e)
        {
            if (SASRequest.GetString("cid") != "")
            {
                Users.DelUser(SASRequest.GetString("cid"));
                BindDataGrid();
            }
        }

        /// <summary>
        /// 设置管理员
        /// </summary>
        protected void setadminclick_ServerClick(object sender, EventArgs e)
        {
            int setadminid = SASRequest.GetInt("sadminid", 0);
            if (SASRequest.GetString("cid") != "" && setadminid > 0)
            {
                Users.UpdateUsersFid(setadminid, SASRequest.GetString("cid"));
                BindDataGrid();
            }
        }

        #region Web 窗体设计器生成的代码

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            string headname = "用户信息列表";
            if (manageType == 1)
            {
                headname = "超级管理员信息列表";
                parmStr = "id = manager";
                add.Visible = false;
                groupid = Convert.ToInt16(ManageGroup.AdminGroup);
            }
            else if (manageType == 2)
            {
                headname = "企业普通管理员信息列表";
                groupid = Convert.ToInt16(ManageGroup.CorpGroup);
                parmStr = "(id <> manager or manager is null) AND gid = " + groupid;
            }
            else if (manageType == 3)
            {
                groupid = Convert.ToInt16(ManageGroup.NormalGroup);
                parmStr = "(id <> manager or manager is null) AND gid = " + groupid;
                DataGrid1.Columns[DataGrid1.Columns.Count - 1].Visible = true;
            }

            DataGrid1.PageIndexChanged += new DataGridPageChangedEventHandler(DataGrid1_PageIndexChanged);
            DataGrid1.GoToPagerButton.Click += new EventHandler(GoToPagerButton_Click);
            DataGrid1.AllowCustomPaging = true;
            DataGrid1.TableHeaderName = headname;
            DataGrid1.Attributes.Add("borderStyle", "0");
            DataGrid1.DataKeyField = "id";
            DataGrid1.ColumnSpan = 12;
            DataGrid1.SaveDSViewState = true;

            check.Click += new EventHandler(check_Click);
            reclick.ServerClick += new EventHandler(reclick_Click);
            pause.Click += new EventHandler(pause_Click);
            start.Click += new EventHandler(start_Click);
            reuserpass.ServerClick += new EventHandler(reuserpass_ServerClick);
            alldel.Click += new EventHandler(alldel_Click);
            setadminclick.ServerClick += new EventHandler(setadminclick_ServerClick);
            setadmin.Visible = false;

            if (!OpAction(OprateAction.Create)) add.Visible = false;
            if (!OpAction(OprateAction.Update)) { 
                pause.Visible = false; start.Visible = false; 
                DataGrid1.Columns[0].Visible = false;
            }
            if (!OpAction(OprateAction.Delete)) { alldel.Visible = false; }
            if (manageType == 3 && OpAction(OprateAction.Update)) setadmin.Visible = true;
        }

        #endregion
    }
}